package com.fingard.dsp.bank.directbank.mtcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @ClassName MTCB01Base
 * @Author Zhengzy
 * @Date 2019/12/19 9:32
 * @Version 1.0
 **/
public class MTCB01Base extends DirectBase implements ITcpClient {
    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            String tmpCharset = getCharset();
            String tmpRecNow = new String(p_receiver.recBytes, 0, p_receiver.readedLength, tmpCharset);
            if (tmpRecNow.contains("</ap>")) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }

    /**
     * 写公共报文头
     *
     * @param xmlTextWriter
     * @param p_bankHeader
     */
    protected void writeCommonHeader(XmlTextWriter xmlTextWriter, MTCB01Header p_bankHeader) {
        xmlTextWriter.writeStartElement("ap");
        xmlTextWriter.writeStartElement("head");
        xmlTextWriter.writeElementString("tr_code", p_bankHeader.trCode, true);// 交易码
        xmlTextWriter.writeElementString("cms_corp_no", bankFront.corpNo, true);// 银企直联客户号
        xmlTextWriter.writeElementString("user_no", "", true);// 用户号
        xmlTextWriter.writeElementString("org_code", "", true);// 机构号
        xmlTextWriter.writeElementString("serial_no", "", true);// 交易流水号 送空  银企直联系统产生
        xmlTextWriter.writeElementString("req_no", p_bankHeader.reqNo, true);// 请求号，企业ERP的流水号
        Date date = new Date();
        xmlTextWriter.writeElementString("tr_acdt", Format.Date8Format.format(date));// 交易日期
        xmlTextWriter.writeElementString("tr_time", Format.Time6Format.format(date));// 交易时间
        xmlTextWriter.writeElementString("channel", "5");// 渠道标识
        xmlTextWriter.writeElementString("sign", "0");// 签名标识
        xmlTextWriter.writeElementString("file_flag", p_bankHeader.fileFlag);// 文件标识
        xmlTextWriter.writeElementString("reserved", "", true);// 保留字段
        xmlTextWriter.writeEndElement();
    }

    /**
     * 发送报文到银行
     *
     * @param p_reqStr
     * @return
     * @throws Exception
     */
    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        byte[] tmpBytes = p_reqStr.getBytes(getCharset());
        int XMLlength = tmpBytes.length + 2;
        // 报体的长度不足10位则右边用空格补足  “123458    ”
        StringBuilder reqContext = new StringBuilder(p_reqStr).insert(0, String.format("%-10d", XMLlength) + "00");
        WriteBankUrlLogLn("请求银行地址" + bankFront.serverIP + ":" + bankFront.serverPort);
        WriteBankLogStep2(reqContext.toString());
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        tmpClient.timeoutMil = 300000;
        TcpReceiver tmpReceiver = tmpClient.sendMessage(reqContext.toString(), getCharset());
        retStr[0] = tmpReceiver.connState;
        retStr[1] = tmpReceiver.getFullString(getCharset());
        if (StringHelper.isNullOrEmpty(retStr[1])) {
            retStr[1] = tmpReceiver.getConnMsg();
        } else {
            retStr[1] = tmpReceiver.getFullString(getCharset());
            int idx = retStr[1].indexOf("<ap>");
            if (idx != -1) {
                retStr[1] = retStr[1].substring(idx);
            }
        }
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    /**
     * 获取字符集
     *
     * @return
     */
    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }
}
